﻿
using System.Collections.Generic;

namespace Pisces
{
	/**
	 * ui界面管理模块
     * created by wangqiang
    **/
	public class UIPageModule : AbstractSingletonModule<UIPageModule>, InterfaceEntityLiftcycle
	{
		/// <summary>
		/// 已经打开的界面map
		/// key:界面名字, value:界面对象
		/// </summary>
		private Dictionary<string, AbstractUIPage> openedPageMap;

		/// <summary>
		/// ui页面创建工厂
		/// </summary>
		private UIPageFactory pageFactory;

		public override void onInit()
		{
			base.onInit();
			
			openedPageMap = new Dictionary<string, AbstractUIPage>();
			pageFactory = new UIPageFactory();
		}

		/// <summary>
		/// 根据界面名字打开界面
		/// </summary>
		/// <param name="pageName"></param>
		/// <returns></returns>
		public AbstractUIPage openPageByPageName(string pageName)
		{
			if (string.IsNullOrEmpty(pageName))
			{
				PiscesLog.warn("打开页面失败,页面名字为空");
				return null;
			}

			AbstractUIPage uiPage = pageFactory.createByPageName(pageName);
			if (uiPage == null)
			{
				PiscesLog.warn("打开页面失败,页面不存在,页面名字：" + pageName);
				return null;
			}

			openedPageMap[pageName] = uiPage;

			return uiPage;
		}

		/// <summary>
		/// 根据界面名字关闭界面
		/// </summary>
		/// <param name="pageName"></param>
		public void closePageByPageName(string pageName)
		{
			if (!openedPageMap.ContainsKey(pageName))
			{
				return;
			}
			
			AbstractUIPage page = openedPageMap[pageName];
			openedPageMap.Remove(pageName);
			
			page.onDestroy();
		}
		
	}
}